<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<jsp:include page="../frame/header.jsp"></jsp:include>

<title>论坛主题管理</title>
<style>
.checkbox{
	padding-left: 0px;
}
div.in-line{
	margin-top:5px;
}
div.in-line label{
	width:70px;
	text-align:right;
}
#searchBtn{
	text-align:right;
}
.form-horizontal .form-actions {
padding-left: 0px;
}
#addBtn{
	margin-left:10px;
}
</style>
</head>

<body>

	<div class="container-fluid">
		<div id="market" class="row-fluid">
			<div>
				<ul class="breadcrumb">
					<li><a href="#">娱乐交友</a> <span class="divider">/</span></li>
					<li><a href="#">树洞论坛</a></li>
				</ul>
			</div>

			<div class="box">
				<div class="box-header well" data-original-title>
					<h2>
						<i class="icon-th"></i> 论坛主题管理
					</h2>

					<div class="box-icon">
						<a href="#" class="btn btn-setting btn-round"><i
							class="icon-cog"></i></a> <a href="#"
							class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						<a href="#" class="btn btn-close btn-round"><i
							class="icon-remove"></i></a>
					</div>
				</div>
				<div class="box-content">
					<div id="search-form">
					<form id="searchForm" method="post" class="form-search span12" action="<c:url value="/admin/forum/topic/search"/>">
						<div class="in-line">
						<label>所属版块：</label>
						<select data-placeholder="全部" id="forums_0" name="forumId" data-rel="chosen">
							<option value="0">全部</option>
							
						</select>
						<input type="hidden" name="pageNum" id="pageNum" value="${searchForm.pageNum }">
						<label>标题：</label>
						<input type="text" name="title" id="title" value="${searchForm.title }" 
								class="input-large search-query">
						</div>
						<div class="in-line">
						<label>发表时间：</label>
						<input class="input-large datepicker"  name="startTime" id="date1" 
										type="text" value="" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至：
						<input class="input-large datepicker" name="endTime" id="date2" 
										type="text" value="" >
						</div>
						<div class="in-line">
						<label>作者：</label>
						<input type="text" name="authors" id="authors" value="${searchForm.authors }" 
								class="input-large search-query">
						<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(多用户名中间请用半角逗号 "," 隔开）</span>
						<a id="addBtn" class="btn btn-primary pull-right" href="<c:url value="/admin/forum/addTopic"/>">发布帖子</a>
						<button type="button" id="searchBtn" class="btn btn-success pull-right">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
						</div>
					</form>
					</div>
					<table id="userInfo" class="table table-striped table-bordered ">
						<thead>
							<tr>
								<th><input type="checkbox" id="ck_all"></th>
								<th>标题</th>
								<th>版块</th>
								<th>作者</th>
								<th>回复</th>
								<th>浏览</th>
								<th>最后发表</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach var="topic" items="${pageModel.result }" varStatus="status">
								<tr>
									<td><input type="checkbox" class="ck_f" value="${topic.topicId }"></td>
									<td>${topic.subject }</td>
									<td>${topic.name }</td>
									<td>${topic.author } </td>
									<td>${topic.replies }</td>
									<td>${topic.views }</td>
									<td>${topic.lastPostDate }</td>
									<td>
										<a href="<c:url value="/admin/forum/topic/${topic.topicId }"/>">编辑</a>
									</td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
					<div id="mypager" class="mypager"></div>
					<div>
						<hr>
					</div>
					
					<div>
						<h2>批量操作</h2>
						
						<form id="batchForm" class="form-horizontal" onsubmit="return validate();" method="post" action="<c:url value="/admin/forum/topic/batch"/>">
							<fieldset>
								<table id="" class="table table-striped ">
									<thead>
										<tr>
											<th></th>
											<th>操作</th>
											<th>选项</th>
										</tr>
									</thead>
									<tbody>
										<tr>
											<td>
												<label class="radio inline">
													<input type="hidden" name="topicIds" id="topicIds">
													<input type="radio" name="batchOption" value="1" >
												</label>
											</td>
											<td>批量移动到版块</td>
											<td>
											 	<select data-placeholder="请选择一个版块" id="forums2_0" name="forumId" data-rel="chosen">
													<option value="0">请选择版块</option>
												</select>
											</td>
										</tr>
										<tr>
											<td>
												<label class="radio inline"> 
													<input type="radio" name="batchOption" value="2">
												</label>
											</td>
											<td>批量置顶</td>
											<td>
												<label class="radio inline">
													<input type="radio" name="upOption" value="0" checked> 解除
												</label>
												<label class="radio inline">
													<input type="radio" name="upOption" value="1">置顶Ⅰ
												</label>
												<label class="radio inline">
													<input type="radio" name="upOption" value="2">置顶Ⅱ
												</label>
												<label class="radio inline">
													<input type="radio" name="upOption" value="3">置顶Ⅲ
												</label>
											</td>
										</tr>
										<tr>
											<td>
												<label class="radio inline"> 
													<input type="radio" name="batchOption" value="3" >
												</label>
											</td>
											<td>批量打开关闭</td>
											<td>
												<label class="radio inline"> 
													<input type="radio" name="closedOption" value="0" checked> 打开
												</label> 
												<label class="radio inline"> 
													<input type="radio" name="closedOption" value="1">关闭
												</label>
											</td>
										</tr>
										<tr>
											<td>
												<label class="radio inline"> 
													<input type="radio" name="batchOption" value="4">
												</label>
											</td>
											<td>批量删除</td>
											<td>
												<label class="checkbox">
													<input type="checkbox" name="delOption" value="1"> 删帖不减用户发帖数和积分
												</label>
											</td>
										</tr>
										<tr>
											<td>
												<label class="radio inline"> 
													<input type="radio" name="batchOption" value="5">
												</label>
											</td>
											<td>批量设置精华</td>
											<td>
												<label class="radio inline">
													<input type="radio" name="digestOption" value="0" checked> 解除
												</label>
												<label class="radio inline">
													<input type="radio" name="digestOption" value="1">精华Ⅰ
												</label>
												<label class="radio inline">
													<input type="radio" name="digestOption" value="2">精华Ⅱ
												</label>
												<label class="radio inline">
													<input type="radio" name="digestOption" value="3">精华Ⅲ
												</label>
											</td>
										</tr>
									</tbody>
								</table>
								<div class="form-actions">
									<button id="saveBtn" type="submit" class="btn btn-primary">提交</button>									
								</div>
								<input type="hidden" name="searchForm.forumId" id="s_forumId" value="0">
								<input type="hidden" name="searchForm.pageNum" id="s_pageNum" value="1">
								<input type="hidden" name="searchForm.title" id="s_title" value="">
								<input type="hidden" name="searchForm.startTime" id="s_startTime" value="">
								<input type="hidden" name="searchForm.endTime" id="s_endTime" value="">
								<input type="hidden" name="searchForm.authors" id="s_authors" value="">
							</fieldset>
						</form>
					</div>
				</div>
			</div>
		</div>
		<!--/fluid-row-->

	</div>
	<!--/.fluid-container-->

	<jsp:include page="../frame/scripts.jsp"></jsp:include>
	<script type="text/javascript">
	var pageNum = "${pageModel.pageNum}";
	var totalPage = "${pageModel.totalPage}";
	var numPerPage = "${pageModel.numPerPage}";
	
	$(document).ready(function(){
				
		$('.datepicker').datepicker('option', 'dateFormat', 'yy-mm-dd'); 
		
		$( "#date1" ).datepicker( "setDate", "${searchForm.startTime}" );
		$( "#date2" ).datepicker( "setDate", "${searchForm.endTime}" );
		
		$('#mypager').bootstrapPaginator({
			currentPage:pageNum,
			totalPages:totalPage,
			numberOfPages:numPerPage,
			onPageClicked:function(event, originalEvent, type,page){
				$("input[name='pageNum']").val(page);
				$("#searchForm").submit();
			}
		});
		
		$('#searchBtn').click(function(){
			$("#searchForm").submit();
		})
		
		//初始化论坛版块
		$.ajax({
			url:'<c:url value="/admin/forum/tree" />',
			dataType:'json',
			success:function(data){
				//搜索表单初始化版块下拉框
				createTree(data,"forums");
				//设置选中
				$("#forums_0").val("${searchForm.forumId}");
				
				//批量操作初始化版块下拉框
				createTree(data,"forums2");
				
				//增加下拉框样式
				//$('[data-rel="chosen"],[rel="chosen"]').chosen();
			}
		})
		
		//全选
		$("#ck_all").change(function(){
			
			var checked = $(this).prop('checked');
			if(checked){
				$(".ck_f").attr('checked',true);
			}else{
				$(".ck_f").attr('checked',false);
			}
			$.uniform.update();
		})
		
	})
	
	function createTree(data,tagId){
		$.each(data,function(i,item){
			//生成节点
			createNode(item,tagId);
			if(item.children.length > 0){
				//生成子节点
				createTree(item.children,tagId);
			}
		})
	}
	
	function createNode(item,tagId){
		if(item.forumId != 2){
			var $parent_select = $("#"+tagId+"_"+item.parentId);
			var str = '';
			if(item.children.length > 0){
				str += '<optgroup id="'+tagId+'_'+item.forumId+'" label="'+item.name+'">';
				str += '</optgroup>';
			}else{
				if(item.type == 1){
					str += '<option value='+item.forumId+'>'+item.name+'</option>';
				}
			}
			
			$parent_select.append(str);
		}
		
	}
	
	function validate(){
		
		var checkedTopic = $(".ck_f:checked");
		if(checkedTopic.length == 0){
			alert("请勾选您要批量操作的论坛主题！");
			return false;
		}
		
		var topicIds = '';
		$.each(checkedTopic,function(i,item){
			topicIds += $(item).val() + ',';
		})
		$("#topicIds").val(topicIds);
		
		var batchOption = $("#batchForm input[name='batchOption']");
		var hasChecked = false;
		var valid = true;
		$.each(batchOption,function(i,item){
			if($(item).prop('checked')){
				hasChecked = true;
				if($(item).val() == '1'){
					if($("#forums2_0").val() == '0'){
						alert("请选择您要批量移动的目标版块!");
						valid = false;
					}
				}
			}
			
		})
		
		if(!hasChecked){
			alert("请选择一种操作！");
			return false;	
		}
		if(!valid){
			return false;
		}
		
		//将searchform内容转移至batchForm
		$("#s_forumId").val($("#searchForm #forums_0").val());
		$("#s_pageNum").val($("#searchForm #pageNum").val());
		$("#s_title").val($("#searchForm #title").val());
		$("#s_startTime").val($("#searchForm #date1").val());
		$("#s_endTime").val($("#searchForm #date2").val());
		$("#s_authors").val($("#searchForm #authors").val());
		
		alert("批量设置成功！");
	}
	</script>
	
</body>
</html>
